//TAG: 暂时允许不被调用的函数出现
#![allow(dead_code)]
use rmpredictdb::model::User;
use std::sync::Mutex;
use tokio::{runtime::Handle, task::block_in_place};

#[derive(Debug)]
pub struct UserState(Mutex<User>);

impl UserState {
    #[inline]
    pub fn new() -> Self {
        Self(Mutex::new(User::default()))
    }

    pub fn get_user(&self) -> Result<User, String> {
        let user = self.0.lock().map_err(|e| e.to_string())?;
        Ok(user.clone())
    }

    pub fn update_username(&self, new_username: String) -> Result<(), String> {
        let mut user = self.0.lock().map_err(|e| e.to_string())?;
        user.set_username(new_username);
        Ok(())
    }
    pub fn update_predict(&self, new_predict: String) -> Result<(), String> {
        let mut user = self.0.lock().map_err(|e| e.to_string())?;
        user.set_last_predict(Some(new_predict));
        Ok(())
    }
    pub fn update_result(&self, new_result: String) -> Result<(), String> {
        let mut user = self.0.lock().map_err(|e| e.to_string())?;
        user.set_last_result(Some(new_result));
        Ok(())
    }
    pub fn update_instance(&self, new_instance: User) -> Result<(), String> {
        let mut old_instance = self.0.lock().map_err(|e| e.to_string())?;
        old_instance.set_id(new_instance.get_id().to_owned());
        old_instance.set_username(new_instance.get_username().to_owned());
        old_instance.set_password(new_instance.get_password().to_owned());
        old_instance.set_last_predict(new_instance.get_last_predict().to_owned());
        old_instance.set_last_result(new_instance.get_last_result().to_owned());

        Ok(())
    }
    pub fn update_password(&self, new_password: String) -> Result<(), String> {
        let mut user = self.0.lock().map_err(|e| e.to_string())?;
        user.set_password(new_password);
        Ok(())
    }

    pub fn verify_password(&self, password: &str) -> Result<bool, String> {
        let user = self.0.lock().map_err(|e| e.to_string())?;
        Ok(user.get_password() == password)
    }
    #[inline]
    pub fn init() -> Self {
        UserState::new()
    }
}

// 注册状态到Tauri

#[derive(Debug, Default)]
pub struct ChildProcessRatatuiState {
    child: Mutex<Option<tokio::process::Child>>,
}

impl ChildProcessRatatuiState {
    #[inline]
    pub fn new() -> Self {
        Self {
            child: Mutex::new(Option::default()),
        }
    }
    pub fn set_ratatui_process(&self, child: tokio::process::Child) {
        let mut child_guard = self.child.lock().unwrap();
        *child_guard = Some(child);
    }
    #[inline]
    pub fn init() -> Self {
        ChildProcessRatatuiState::new()
    }
    pub async fn kill(&self) {
        let mut child_guard = self.child.lock().unwrap();
        if let Some(child) = child_guard.as_mut() {
            println!("process:{}", child.id().unwrap());
            let mut kill_process = tokio::process::Command::new("taskkill")
                .args(["/pid", &child.id().unwrap().to_string(), "/T", "/F"])
                .kill_on_drop(true)
                .spawn()
                .unwrap();
            block_in_place(move || {
                Handle::current().block_on(async move { kill_process.wait().await.unwrap() });
            });
        }
        *child_guard = None
    }
}
